package cn.yshujia.common;

import com.baomidou.mybatisplus.extension.p6spy.StdoutLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description sql 日志
 */

@Slf4j
public class P6SpyLog extends StdoutLogger {
	
	
	
	public void logText(String text) {
		// SQL 语句不为空且不是查询语句
//		if (!ObjectUtils.isEmpty(text) && !StringUtils.containsAnyIgnoreCase(text, "select")) {
//			print(getFormatSql(text));
//		}
		if (!ObjectUtils.isEmpty(text)) {
			print(getFormatSql(text));
		}
	}
	
	private static String getFormatSql(String sql) {
		sql = sql.replaceAll("\\s+", " ");
		sql = sql.replaceAll("\n", "");
		sql = sql.replaceAll("FROM|from", "\nFROM");
		sql = sql.replaceAll("WHERE|where", "\nWHERE");
		sql = sql.replaceAll("GROUP|group", "\nGROUP");
		sql = sql.replaceAll("order by|ORDER BY", "\nORDER BY");
		sql = sql.replaceAll("JOIN|join", "\nJOIN");
		sql = sql.replaceAll("HAVING|having", "\nHAVING");
		return sql;
	}
	
	private void print(String msg) {
		String s = msg + "\n";
		log.info(s);
	}
	
	
}